<?php
/**
 * source url:http://blog.csdn.net/xiaojun1288/article/details/7480274
 * 
 * Dynamically load modules, models, and configurations in Yii
 * http://www.chrisgmyr.com/2011/09/dynamically-load-modules-models-and-configurations-in-yii/
 * 
 * @author Elwin <34557049@qq.com>
 * @license released under dual license BSD License and LGP License
 * @package Admin
 * @version 1.0
 */
class AdminModule extends CWebModule
{
	private $_assetsUrl;
	public function init()
	{
		// 导入模块级模型和组件
		$this->setImport ( array (
				'admin.models.*','admin.components.*' 
		) );
		
		// $this->layout = 'main';
		
		// 默认控件器名称
		// $this->defaultController = '';
		
		// 导入文件
		// $this->setImport(array(
		// 'userGroups.models.*',
		// 'userGroups.components.*',
		// ));
		
		// 导入jquery文件
		// Yii::app()->clientScript->registerCoreScript('jquery');
		
		// ajax请求的时候不载入css,js文件
		if (! Yii::app ()->request->isAjaxRequest)
		{
			// Yii::app()->clientScript->registerCssFile(Yii::app()->assetManager->publish(Yii::getPathOfAlias('userGroups')
		// . '/css').'/userGroups.css');
			// Yii::app()->clientScript->registerScriptFile(Yii::app()->assetManager->publish(Yii::getPathOfAlias('userGroups')
		// . '/js/userGroups.js'));
		}
	}
	public function getAssetsUrl()
	{
		if ($this->_assetsUrl === null)
			$this->_assetsUrl = Yii::app ()->getAssetManager ()->publish ( Yii::getPathOfAlias ( 'application.modules.admin.assets' ) );
		return $this->_assetsUrl;
	}
	public function setAssetsUrl($value)
	{
		$this->_assetsUrl = $value;
	}
	public function beforeControllerAction($controller, $action)
	{
		if (parent::beforeControllerAction ( $controller, $action ))
		{
			// Yii::app()->db->tablePrefix
			// print_r(Yii::app()->db->getSchema()->getTableNames());
			// Yii::app()->request->redirect
			// 只有admin角色才可以访问admin模块
			if (! Yii::app ()->authManager->checkAccess ( "admin", Yii::app ()->user->id ))
			{
				// echo '不是admin角色';
				return true;
				// throw new CHttpException(403,Yii::t('yii','You are not au-
				// thorized to perform this action.'));
			} else
				return true;
		} else
		{
			return false;
		}
	}
}
?>